package com.qing.thread.practice;

import com.qing.thread.util.DownloadPicUtil;

/**
 * 模拟多线程下载图片
 * 1：编写下载器
 * 2：继承Thread
 * 3：重写run方法
 * 4：调用start方法启动线程
 * <p>
 * 结果：顺序不固定，证明线程是由CPU调度进行的
 * 落日.jpg下载完咯！
 * 蓝天.jpg下载完咯！
 * 大海.jpg下载完咯！
 * 雪山.jpg下载完咯！
 */
public class ThreadDownloadPic implements Runnable {
    private String url;
    private String name;

    public ThreadDownloadPic(String url, String name) {
        this.url = url;
        this.name = name;
    }

    @Override
    public void run() {
        new DownloadPicUtil().downloadPic(url, name);
        System.out.println(name + "下载完咯！");
    }


    public static void main(String[] args) {
        ThreadDownloadPic t1 = new ThreadDownloadPic("https://lmg.jj20.com/up/allimg/1113/052420110515/200524110515-1-1200.jpg", "雪山.jpg");
        ThreadDownloadPic t2 = new ThreadDownloadPic("https://img1.baidu.com/it/u=481689849,3217240113&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500", "大海.jpg");
        ThreadDownloadPic t3 = new ThreadDownloadPic("https://img2.baidu.com/it/u=2529031386,4198239090&fm=253&fmt=auto&app=138&f=JPEG?w=889&h=500", "落日.jpg");
        ThreadDownloadPic t4 = new ThreadDownloadPic("https://img2.baidu.com/it/u=3812026306,1183665387&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=500", "蓝天.jpg");
        //继承Thread这样启动
        // t1.start();
        // t2.start();
        // t3.start();
        // t4.start();

        //实现Runnable这样启动
        new Thread(t1).start();
        new Thread(t2).start();
        new Thread(t3).start();
        new Thread(t4).start();
    }
}
